package com.zyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyg.dao.PermissionMapper;
import com.zyg.pojo.Permission;
import com.zyg.pojo.RolePermission;
import com.zyg.pojo.UserRole;
import com.zyg.service.PermissionService;
import com.zyg.service.RolePermissionService;
import com.zyg.service.RoleService;
import com.zyg.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {
    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private RolePermissionService rolePermissionService;
    @Override
    public List<Permission> list() {

        List<Permission> permissions = baseMapper.selectList(null);
        // 创建根集合
        List<Permission> root = permissions.stream().filter(p -> p.getParentId() == 0).toList();
        // 递归给根集合设置子节点
        root.forEach(p -> {
            p.setChildren(getChildren(p.getId(), permissions));
            p.setIsParent(p.getChildren().size() > 0);
        });
        return root;
    }

    private List<Permission> getChildren(Integer id, List<Permission> permissions) {
        List<Permission> children = new ArrayList<>();
        permissions.forEach(p -> {
            if (p.getParentId() != null && p.getParentId().equals(id)) {
                p.setChildren(getChildren(p.getId(), permissions));
                p.setIsParent(p.getChildren().size() > 0);
                children.add(p);
            }
        });
        return children;
    }
    public List<Permission> getMenuList() {
        List<Permission> permissions = baseMapper.selectList(null);
        List<Permission> permissionList = permissions.stream().filter(p -> !p.getMenuType().equals("F")).toList();

        // 创建根集合
        List<Permission> root = permissionList.stream().filter(p -> p.getParentId() == 0).toList();
        // 递归给根集合设置子节点
        root.forEach(p -> {
            p.setChildren(getChildren(p.getId(), permissions));
            p.setIsParent(p.getChildren().size() > 0);
        });
        return root;
    }

    @Override
    public List<Permission> getMenuListByUserId(Integer loginId) {
        // 根据用户id查询用户角色
        QueryWrapper qw = new QueryWrapper();
        qw.eq("u_id", loginId);
        List<UserRole> userRoleList = userRoleService.list(qw);
        List<Integer> roleIds = userRoleList.stream().map(UserRole::getRId).toList();
        // 遍历角色id 获取对应的权限id
        List<Integer> permissionIds = rolePermissionService.list(new QueryWrapper<RolePermission>().in("r_id", roleIds)).stream().map(RolePermission::getPId).toList();
        // 根据权限id集合获取对应的权限名称
        List<Permission> permissions = listByIds(permissionIds);

        List<Permission> permissionList = permissions.stream().filter(p -> !p.getMenuType().equals("F")).toList();

        // 创建根集合
        List<Permission> root = permissionList.stream().filter(p -> p.getParentId() == 0).toList();
        // 递归给根集合设置子节点
        root.forEach(p -> {
            p.setChildren(getChildren(p.getId(), permissions));
            p.setIsParent(p.getChildren().size() > 0);
        });
        return root;
    }


}
